<?php

namespace backend\controllers;

use Yii;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\captcha\CaptchaAction;

use common\libs\ServerInfo;
use common\models\Comment;
use common\models\FriendlyLink;
use backend\models\Article;
use backend\models\Comment as BackendComment;
use backend\models\LoginForm;
use backend\models\Menu;
use frontend\models\User;

use common\helpers\PHPTree;
use tests\models\Tree;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;

use backend\models\Category;

/**
 * Site controller
 */
class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'captcha', 'error'],
                        'allow'   => true,
                    ],
                    [
                        'actions' => ['logout', 'index', 'main'],
                        'allow'   => true,
                        'roles'   => ['@'],
                    ],
                ],
            ],
            'verbs'  => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error'   => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class'     => CaptchaAction::className(),
                'backColor' => 0x66b3ff, //背景颜色
                'maxLength' => 4, //最大显示个数
                'minLength' => 4, //最少显示个数
                'padding'   => 6, //验证码字体大小，数值越小字体越大
                'height'    => 34, //高度
                'width'     => 100, //宽度
                'foreColor' => 0xffffff, //字体颜色
                'offset'    => 13, //设置字符偏移量
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->renderPartial('index');
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            $model->password = '';

            return $this->renderPartial(
                'login',
                [
                    'model' => $model,
                ]
            );
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * @Notes 切换语言
     * @Interface actionLanguage
     *
     *
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2019-12-19 9:05
     */
    public function actionLanguage()
    {
        $language = Yii::$app->getRequest()->get('lang');
        if (isset($language)) {
            $session = Yii::$app->getSession();
            $session->set('language', $language);
        }
        $this->goBack(Yii::$app->getRequest()->headers['referer']);
    }


    /**
     * 主页
     *
     * @return string
     */
    public function actionMain()
    {
        return $this->render('main');
    }


    /**
     * @Notes   临时添加后台菜单入口
     * @Interface actionCtree
     *
     *
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2019-12-19 20:30
     */
    public function actionCtree()
    {

////        $_Menu = new Menu(['name' => '后台菜单']);
////        $_Menu->makeRoot();
//        $_Menu   = Menu::findOne(['id' => '2']);
//        $_Menu_c = new Menu(['name' => '基础设置']);
//        $_Menu_c->appendTo($_Menu);
//        $_Menu_c = new Menu(['name' => '自定义设置']);
//        $_Menu_c->appendTo($_Menu);
//        $_Menu_c = new Menu(['name' => 'SMTP设置']);
//        $_Menu_c->appendTo($_Menu);
//        $_Menu_c = new Menu(['name' => '操作记录']);
//        $_Menu_c->appendTo($_Menu);
//        $_Menu_c = new Menu(['name' => '错误日志']);
//        $_Menu_c->appendTo($_Menu);

//        echo "ok";
//        exit;
//        $_Menu = Menu::findOne(['id' => '1']);
////        $_Menu_ = $_Menu->toArray();
////        var_dump($_Menu);exit;
////        $children = $_Menu->children()->asArray()->all();
//        $_children = $_Menu->children()->all();
////        $children  = ArrayHelper::toArray($_children);
//        foreach ($_children as $key => $Model) {
//            $children[$key]=$Model->toArray();
//        }
//        $children=PHPTree::makeTree($children);
//
//        var_dump($children);
//        $_Menu = Menu::findOne(['id' => '1']);
//        $_children = $_Menu->children()->all();
//        foreach ($_children as $key => $Model) {
//            $children[$key] = $Model->toArray();
//        }
//        $children=PHPTree::makeTreeForHtml($children);
//        $children=Menu::getBackendMenu('1');
//        echo $children;
//        exit;
//        $array=['关于我们',,,,]
//        $_Category = new Category(['name' => '信息中心']);
//        $_Category->makeRoot();


    }
}
